//
//  ALPS_PlayMusicTool.m
//  Alps
//
//  Created by dllo on 16/6/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ALPS_PlayMusicTool.h"

static ALPS_PlayMusicTool *player;
static ALPS_PlayMusicModel *playingMusicModel;

@interface ALPS_PlayMusicTool()

/**
 *  当前item
 */
@property (nonatomic, retain) AVPlayerItem *currentItem;

@end


@implementation ALPS_PlayMusicTool

+ (instancetype)manager
{
    static dispatch_once_t once_Token;
    dispatch_once(&once_Token, ^{
        //初始化播放器
        player = [[ALPS_PlayMusicTool alloc] init];
    });
    return player;
}

#pragma mark -- 初始化方法(仅运行一次)
- (id)init
{
    self = [super init];
    if (self)
    {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            self.player = [AVPlayer playerWithPlayerItem:self.currentItem];
            self.player.volume = 0.4;
        });
        isPlaying = NO;
    }
    return self;
}

#pragma mark -- 播放音乐
- (void)playCurrentMusic
{
    if (isPlaying)
    {
        return;
    }
    if (!isPlaying)
    {
        [self.player replaceCurrentItemWithPlayerItem:self.currentItem];
        [self.player play];
        isPlaying = YES;
    }
}

#pragma mark -- 暂停音乐
- (void)pauseCurrentMusic
{
    if (isPlaying)
    {
        [self.player pause];
        isPlaying = NO;
        return;
    }
    if (!isPlaying)
    {
        return;
    }
}

#pragma mark -- 获取播放时长
- (NSString *)getCurrentTime
{
    CGFloat currentTime = CMTimeGetSeconds([self.player currentTime]);
    
    int minutes = (int)currentTime / 60;
    int seconds = (int)currentTime - minutes * 60;
    
    NSString *timeString;
    
    if (minutes >= 0 && seconds >= 0)
    {
        if (minutes < 1 && seconds < 10)
        {
            timeString = [NSString stringWithFormat:@"00:0%d", seconds];
        }
        else if (minutes <= 1 && seconds >= 10)
        {
            timeString = [NSString stringWithFormat:@"00:%d", seconds];
        }
        else if (minutes >= 1 && seconds < 10)
        {
            timeString = [NSString stringWithFormat:@"%d:0%d", minutes, seconds];
        }
        else if (minutes >= 1 && seconds >= 10)
        {
            timeString = [NSString stringWithFormat:@"%d:%d", minutes, seconds];
        }
    }
    else
    {
        timeString = @"00:00";
    }
    
    return timeString;
}

- (void)setModel:(ALPS_PlayMusicModel *)model
{
    isPlaying = YES;
    if ([playingMusicModel.playUrl64 isEqualToString:model.playUrl64])
    {
        return;
    }
    else
    {
        self.currentItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:model.playUrl64]];
        [self.player replaceCurrentItemWithPlayerItem:self.currentItem];
        [self.player play];
        playingMusicModel = model;
    }
}

- (void)playRadioWithURL:(NSString *)urlString
{
    self.currentItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:urlString]];
    [self.player replaceCurrentItemWithPlayerItem:self.currentItem];
    [self.player play];
    isPlaying = YES;
}

@end
